package com.unicloud.nifi.processor;

import org.apache.nifi.components.PropertyDescriptor;
import org.apache.nifi.flowfile.FlowFile;
import org.apache.nifi.processor.*;
import org.apache.nifi.processor.exception.ProcessException;
import org.apache.nifi.processor.util.StandardValidators;

import javax.mail.Store;
import java.util.*;

public class TriggerTestProcessor extends AbstractProcessor {

    private List<PropertyDescriptor> properties;
    private Set<Relationship> relationships;
    private Store store;

    public static final PropertyDescriptor mail_user_name = new PropertyDescriptor.Builder()
            .name("emailUserName")
            .required(true)
            .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
            .build();
    public static final PropertyDescriptor mail_password = new PropertyDescriptor.Builder()
            .name("emailPassword")
            .required(true)
            .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
            .build();

    public static final Relationship SUCCESS = new Relationship.Builder()
            .name("SUCCESS")
            .description("Succes relationship")
            .build();


    @Override
    public void init(final ProcessorInitializationContext context )  {
        ArrayList<PropertyDescriptor> properties = new ArrayList<>();
        properties.add(mail_user_name);
        properties.add(mail_password);
//        防止多线程ADD
        this.properties = Collections.unmodifiableList(properties);
        Set<Relationship> relationships = new HashSet<>();
        relationships.add(SUCCESS);
//        防止多线程ADD
        this.relationships = Collections.unmodifiableSet(relationships);
    }


    @Override
    public void onTrigger(ProcessContext processContext, ProcessSession processSession) throws ProcessException {

        FlowFile flowFile = processSession.create();
        getLogger().warn("input TriggerTestProcessor trigger ... ...");
        String mail_user_name_Str = processContext.getProperty(mail_user_name).getValue();
        getLogger().warn(" TriggerTestProcessor mail_user_name===>"+mail_user_name_Str);

        processSession.transfer(flowFile, SUCCESS);

    }


    @Override
    public Set<Relationship> getRelationships(){
        return relationships;
    }

    @Override
    public List<PropertyDescriptor> getSupportedPropertyDescriptors(){
        return properties;
    }

}
